/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gisgraph.components;

import edu.uci.ics.jung.graph.util.Pair;
import java.util.Collection;
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.jgrapht.experimental.dag.DirectedAcyclicGraph.CycleFoundException;
import org.jgrapht.graph.DefaultEdge;

/**
 *
 * @author Slawek
 */
public class JDirectedGraphAdapter {
    
    edu.uci.ics.jung.graph.DirectedGraph<Integer, GISEdge> jungGraph;
            
    public JDirectedGraphAdapter(edu.uci.ics.jung.graph.DirectedGraph<Integer,GISEdge> jungGraph)
    {
        this.jungGraph = jungGraph;
    }
    
    public boolean isAcyclic()
    {        
        DirectedAcyclicGraph<Integer,DefaultEdge> g = 
                new DirectedAcyclicGraph<Integer, DefaultEdge>(DefaultEdge.class);
        
        Collection<Integer> vertices =  jungGraph.getVertices();
        Collection<GISEdge> edges = jungGraph.getEdges();
        
        for (Integer vertex : vertices) {
            g.addVertex(vertex);
        }
        
        for (GISEdge edge : edges) {
         
            Pair<Integer> endpoints = jungGraph.getEndpoints(edge);
            
            try 
            {
                g.addDagEdge(endpoints.getFirst(), endpoints.getSecond());
            } 
            catch (CycleFoundException ex) {
                return false;
            }
        }
        
        return true;
    }
    
}
